package com.ztsoft.user.controller.project;

import com.ztsoft.user.pojo.entity.project.Choose;
import com.ztsoft.user.pojo.enums.ResultCode;
import com.ztsoft.user.pojo.vo.project.ChooseRequest;
import com.ztsoft.user.service.project.ChooseService;
import com.ztsoft.user.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Slf4j
@CrossOrigin("*")
@RestController
@RequestMapping("/choose")
@Api(tags = "订单控制层")
public class ChooseController {

    @Resource
    private ChooseService chooseService;

    @GetMapping("/getProject/{pageSize}/{currentPage}")
    public Result<Object> getProject(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "region", required = false) String region, @RequestParam(value = "infoNumber", required = false) String infoNumber) {
        return chooseService.getProject(currentPage, pageSize, name, region, infoNumber);
    }

    // 用户查看自建暂存订单
    @GetMapping("/getChoose/{pid}/{pageSize}/{currentPage}")
    public Result<Object> getChoose(@PathVariable("pid") String pid, @PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize) {
        return chooseService.getChoose(pid, currentPage, pageSize);
    }

    // 用户添加订单
    @PostMapping("/addChoose/{pid}")
    public Result<Object> addChoose(@PathVariable("pid") String pid, @RequestBody String[] ids) {
        return chooseService.addChoose(pid, ids);
    }

    // 用户删除订单
    // @PostMapping("/deleteChoose")
    public Result<Object> deleteChoose(@RequestBody String[] ids) {
        return chooseService.deleteChoose(ids);
    }

    // 清空采购单
    // @DeleteMapping("/deleteChooseAll/{pid}")
    public Result<Object> deleteChooseAll(@PathVariable("pid") String pid) {
        return chooseService.deleteAll(pid);
    }

    // 用户修改订单（填写信息、填写班组）
    @PutMapping("/updateChoose")
    public Result<Object> updateChoose(@RequestBody ChooseRequest chooseRequest) {
        return chooseService.updateChoose(chooseRequest);
    }

    // 获取需求单位联系人
    @GetMapping("/getContact")
    public Result<Object> getContact() {
        return chooseService.getContact();
    }

    // 提交订单
    @PutMapping("/commitChoose/{pid}")
    public Result<Object> commitChoose(@PathVariable("pid") String pid) {
        return chooseService.commitChoose(pid);
    }

    // 获取个人订单信息
    @GetMapping("/getSelfChoose/{pid}/{pageSize}/{currentPage}")
    public Result<Object> getSelfChoose(@PathVariable("pid") String pid, @PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize, @RequestParam(value = "state", defaultValue = "0", required = false) Integer state, @RequestParam(value = "orderId", required = false) String orderId, @RequestParam(value = "orderName", required = false) String orderName, @RequestParam(value = "batch", required = false) String batch, @RequestParam(value = "projectName", required = false) String projectName, @RequestParam(value = "cancel", required = false) Boolean cancel) {
        return chooseService.getSelfChoose(pid, currentPage, pageSize, state, orderId, orderName, batch, projectName, cancel);
    }

    // 审核人员（匹配、审核）获取订单
    @GetMapping("/getOperationChoose/{pid}/{pageSize}/{currentPage}/{type}")
    public Result<Object> getOperationChoose(@PathVariable("pid") String pid, @PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize, @PathVariable("type") Integer type, @RequestParam(value = "orderId", required = false) String orderId, @RequestParam(value = "type2", required = false) Integer type2, @RequestParam(value = "orderName", required = false) String orderName, @RequestParam(value = "batch", required = false) String batch, @RequestParam(value = "projectName", required = false) String projectName, @RequestParam(value = "cancel", required = false) Boolean cancel, @RequestParam(value = "demandUnit", required = false) String demandUnit) {
        return chooseService.getOperationChoose(pid, currentPage, pageSize, type, orderId, type2, orderName, batch, projectName, cancel, demandUnit);
    }

    // 审核订单
    @PutMapping("/auditChoose/{orderId}/{type}")
    public Result<Object> auditChoose(@PathVariable("orderId") String orderId, @PathVariable("type") String type, @RequestBody Choose choose) {
        return chooseService.auditChoose(orderId, type, choose);
    }

    // 订单作废或申请变更
    @PostMapping("/cancelChoose/{orderId}/{type}")
    @ApiOperation(value = "订单作废或申请变更", notes = "订单作废或申请变更")
    public Result<Object> cancelChoose(@PathVariable("orderId") String orderId, @PathVariable("type") int type, @RequestBody MultipartFile file) {
        return chooseService.cancelChoose(orderId, type, file);
    }

    // 订单驳回修改
    @PutMapping("/preserveChoose")
    @ApiOperation(value = "订单驳回修改", notes = "订单驳回修改")
    public Result<Object> preserveChoose(@RequestBody ChooseRequest chooseRequest) {
        return chooseService.preserveChoose(chooseRequest);
    }

    // 导出所有审批清单excel
    @PostMapping("/exportChooseList/{pid}/{type}")
    public void exportChooseList(@RequestBody String[] orderIds, @PathVariable("pid") String pid, @PathVariable("type") int type, HttpServletResponse response) {
        chooseService.exportChooseList(orderIds, pid, type, response);
    }

    // 查看订单详情
    @GetMapping("/getOrderDetails/{orderId}/{pageSize}/{currentPage}")
    public Result<Object> getOrderDetails(@PathVariable("orderId") String orderId, @PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize) {
        return chooseService.getOrderDetails(orderId, currentPage, pageSize);
    }

    // 修改匹配时间及批次
    @PutMapping("updateChooseMatchTimeAndBatch")
    public Result<Object> updateChooseMatchTimeAndBatch(@RequestBody ChooseRequest chooseRequest) {
        return chooseService.updateChooseMatchTimeAndBatch(chooseRequest);
    }

    // 导出审批单word
    @PostMapping("/exportApprovalForm/{pid}")
    public void exportApprovalForm(@PathVariable("pid") String pid, @RequestBody String[] orderIds, HttpServletResponse response) {
        chooseService.exportApprovalForm(pid, orderIds, response);
    }

    // 导出匹配单通知书word
    @PostMapping("/generateTemplateZip")
    public void generateTemplateZip(@RequestBody String[] orderIds, HttpServletResponse response) {
        chooseService.generateTemplateZip(orderIds, response);
    }

    // 查看附件,下载
    @PostMapping("/enclosureDisplay/{orderId}")
    public void enclosureDisplay(HttpServletResponse response, @PathVariable("orderId") String orderId) {
        chooseService.enclosureDisplay(response, orderId);
    }

    // 查看额度信息
    @GetMapping("/getChooseQuotaInfo/{orderId}")
    public Result<Object> getChooseQuotaInfo(@PathVariable("orderId") String orderId) {
        return chooseService.getChooseQuotaInfo(orderId);
    }

    // 获取项目的所有匹配批次
    @GetMapping("/queryBatch/{pid}")
    public Result<Object> queryBatch(@PathVariable("pid") String pid) {
        List<Map<String, String>> list = chooseService.queryBatch(pid);
        return Result.returnDataMsg(ResultCode.SUCCESS.getMsg(), ResultCode.SUCCESS.getCode(), list);
    }

    // 获取项目的所有匹配批次
    @GetMapping("/querySubcontracted/{pid}")
    public Result<Object> querySubcontracted(@PathVariable("pid") String pid) {
        List<Map<String, String>> list = chooseService.querySubcontracted(pid);
        return Result.returnDataMsg(ResultCode.SUCCESS.getMsg(), ResultCode.SUCCESS.getCode(), list);
    }

    // 统计分析
    @GetMapping("/queryStatistics/{pid}/{pageSize}/{currentPage}/{type}")
    public Result<Object> queryStatistics(@PathVariable("pid") String pid, @PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize, @PathVariable("type") Integer type, @RequestParam(value = "batch", required = false) String batch, @RequestParam(value = "demandUnit", required = false) String demandUnit, @RequestParam(value = "packageName", required = false) String packageName, @RequestParam(value = "subcontract", required = false) String subcontract, @RequestParam(value = "subcontracted", required = false) String subcontracted, @RequestParam(value = "startTime", required = false) String startTime, @RequestParam(value = "endTime", required = false) String endTime) {
        return chooseService.queryStatistics(pid, currentPage, pageSize, type, batch, demandUnit, packageName, subcontract, subcontracted, startTime, endTime);
    }

    // 统计分析导出数据excel
    @PostMapping("/exportStatistics/{pid}/{type}")
    public void exportStatistics(@PathVariable("pid") String pid, @PathVariable("type") Integer type, @RequestBody Choose choose, HttpServletResponse response) {
        chooseService.exportStatistics(pid, type, choose, response);
    }

    // 订单退回
    @PutMapping("/fallbackChoose")
    public Result<Object> fallbackChoose(@RequestBody ChooseRequest chooseRequest) {
        return chooseService.fallbackChoose(chooseRequest);
    }

}